// 搜索和筛选功能类型定义

import { Content, ContentType } from '../../types/document';

// 搜索配置
export interface SearchConfig {
  query: string;
  filters: SearchFilters;
  sortBy: 'relevance' | 'date' | 'title' | 'size';
  sortOrder: 'asc' | 'desc';
}

// 筛选条件
export interface SearchFilters {
  contentType?: ContentType[];
  dateRange?: {
    start: number;
    end: number;
  };
  tags?: string[];
  minSize?: number;
  maxSize?: number;
  hasAttachments?: boolean;
}

// 搜索结果
export interface SearchResult {
  document: Content;
  score: number;
  highlights: SearchHighlight[];
  matchedFields: string[];
}

// 搜索高亮
export interface SearchHighlight {
  field: 'title' | 'content';
  text: string;
  start: number;
  end: number;
}

// 标签数据模型
export interface Tag {
  id: string;
  name: string;
  color: string;
  description?: string;
  parentId?: string; // 支持层级标签（可选）
  usageCount: number;
  createdAt: number;
  updatedAt: number;
}

// 文档标签关联
export interface DocumentTag {
  documentId: string;
  tagId: string;
  addedAt: number;
}

// 搜索历史
export interface SearchHistory {
  id: string;
  query: string;
  filters: SearchFilters;
  resultCount: number;
  searchTime: number;
  timestamp: number;
}

// 查询词
export interface QueryTerm {
  term: string;
  weight: number;
  field?: 'title' | 'content' | 'both';
}

// 搜索索引项
export interface SearchIndexItem {
  id: string; // 文档ID
  title: string; // 标题文本
  content: string; // 内容文本
  tokens: string[]; // 分词结果
  tfidf: {[token: string]: number}; // TF-IDF权重
  lastIndexed: number; // 最后索引时间
}

// 服务结果封装
export interface SearchServiceResult<T> {
  success: boolean;
  data?: T;
  error?: string;
}

// 搜索服务接口
export interface SearchService {
  // 全文搜索
  search(config: SearchConfig): Promise<SearchServiceResult<SearchResult[]>>;
  
  // 搜索建议
  getSuggestions(query: string): Promise<string[]>;
  
  // 搜索历史
  getSearchHistory(): Promise<SearchHistory[]>;
  addSearchHistory(query: string, filters: SearchFilters, resultCount: number): Promise<void>;
  clearSearchHistory(): Promise<void>;
  
  // 索引管理
  rebuildIndex(): Promise<void>;
  updateDocumentIndex(document: Content): Promise<void>;
  removeDocumentIndex(documentId: string): Promise<void>;
}

// 标签服务接口
export interface TagService {
  // 标签CRUD
  createTag(name: string, color: string, description?: string): Promise<Tag>;
  updateTag(tagId: string, updates: Partial<Tag>): Promise<void>;
  deleteTag(tagId: string): Promise<void>;
  getTag(tagId: string): Promise<Tag | null>;
  getAllTags(): Promise<Tag[]>;
  
  // 文档标签关联
  addTagToDocument(documentId: string, tagId: string): Promise<void>;
  removeTagFromDocument(documentId: string, tagId: string): Promise<void>;
  getDocumentTags(documentId: string): Promise<Tag[]>;
  getTagDocuments(tagId: string): Promise<string[]>;
  
  // 标签统计和建议
  getTagUsageStats(): Promise<{tagId: string; count: number}[]>;
  suggestTags(content: string): Promise<string[]>;
  getPopularTags(limit?: number): Promise<Tag[]>;
}

// 存储键定义
export const SEARCH_STORAGE_KEYS = {
  SEARCH_INDEX: 'rmzf:search:index',
  TAGS: 'rmzf:search:tags',
  DOCUMENT_TAGS: 'rmzf:search:document_tags',
  SEARCH_HISTORY: 'rmzf:search:history',
  SEARCH_CONFIG: 'rmzf:search:config'
} as const;

// 默认搜索配置
export const DEFAULT_SEARCH_CONFIG: SearchConfig = {
  query: '',
  filters: {},
  sortBy: 'relevance',
  sortOrder: 'desc'
};

// 预设标签颜色
export const TAG_COLORS = [
  '#f50', '#2db7f5', '#87d068', '#108ee9',
  '#f04134', '#00a854', '#ffbf00', '#00a2ae',
  '#f56a00', '#722ed1', '#eb2f96', '#52c41a'
] as const;

// 搜索限制配置
export const SEARCH_LIMITS = {
  MAX_QUERY_LENGTH: 200,
  MAX_RESULTS: 1000,
  MAX_SEARCH_HISTORY: 50,
  MAX_TAGS_PER_DOCUMENT: 20,
  MAX_TAG_NAME_LENGTH: 50,
  MAX_TAG_DESCRIPTION_LENGTH: 200
} as const;